function [CMA_out, CMA_taps]=CMMA_1(CMA_in,CMA_taps)
%% M阶数 MZ 16QAM
T=1;
Constell=4;
CMA_in=sqrt((2^Constell-1)/1.5)*CMA_in/sqrt(mean(abs((CMA_in)).^2)); % 功率归一化
Ts_map_output=qammod(randi([0 15],1,262144),16);
Ts_map_output=sqrt((2^Constell-1)/1.5)*Ts_map_output/sqrt(mean(abs((Ts_map_output)).^2));
% Ts_map_output=round(Ts_map_output*10);
R=unique(abs(Ts_map_output));
Ref1=0.5*(R(1)+R(2));
Ref2=0.5*(R(3)-R(1));
Ref3=0.5*(R(3)-R(2));
% figure;plot(CMA_in,'.');
% CMA_taps=zeros(1,51);
% CMA_taps(ceil(length(CMA_taps)/2))=1;
CMA_taps=CMA_taps/max(abs(reshape(CMA_taps,1,[])));
CMA_len = length(CMA_taps);
CMA_taps = reshape(CMA_taps,[],1);
CMA_in = [zeros(1,floor(CMA_len/2)),CMA_in,zeros(1,floor(CMA_len/2)),];
CMA_out = zeros(1,length(CMA_in)-2*floor(CMA_len/2));
u=3e-4;
%% 16QAM

for num_fg = ceil(CMA_len/2):T:length(CMA_in)/2-floor(CMA_len/2)
            CMA_out(num_fg-floor(CMA_len/2))=CMA_in(num_fg-floor(CMA_len/2):num_fg+floor(CMA_len/2))*CMA_taps;
            Y=CMA_out(num_fg-floor(CMA_len/2));           
            err1= sign( abs(abs(Y)-Ref1)-Ref2)* sign(abs(Y)-Ref1)*sign(Y);
            err(num_fg-floor(CMA_len/2))=abs( abs(abs(Y)-Ref1)-Ref2)- Ref3;
            CMA_taps=CMA_taps-u*err1*err(num_fg-floor(CMA_len/2))*(CMA_in(num_fg-floor(CMA_len/2):num_fg+floor(CMA_len/2)))';
           err2(num_fg-floor(CMA_len/2))=abs(abs(Y)-Ref1)-Ref2;
           err4(num_fg-floor(CMA_len/2))=abs(Y)-Ref1;
end
figure;plot(abs(err),'.');
% figure;plot((err2),'.');
% figure;plot((err4),'.');
clear err;
clear err1;
CMA_out = zeros(1,length(CMA_in)-2*floor(CMA_len/2));
% figure;plot(CMA_out,'.')
for num_fg = ceil(CMA_len/2):T:length(CMA_in)-floor(CMA_len/2)
            CMA_out(num_fg-floor(CMA_len/2))=CMA_in(num_fg-floor(CMA_len/2):num_fg+floor(CMA_len/2))*CMA_taps;
            Y=CMA_out(num_fg-floor(CMA_len/2));           
            err1=  sign( abs(abs(Y)-Ref1)-Ref2)* sign(abs(Y)-Ref1)*sign(Y);
            err(num_fg-floor(CMA_len/2))=abs( abs(abs(Y)-Ref1)-Ref2)- Ref3;
            CMA_taps=CMA_taps-u*err1*err(num_fg-floor(CMA_len/2))*CMA_in(num_fg-floor(CMA_len/2):num_fg+floor(CMA_len/2))';
           
end
figure;plot(abs(err),'.');
clear err;
clear err1;figure;plot(CMA_out(1+100:T:length(CMA_out)/2-100),'.');title('CMMA-out');
CMA_out=CMA_out(1+100:T:end-100);